package cn.com.czpb.client.module.news.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.app.MyActivitymanager;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.model.news.NewsListEntity;
import cn.com.czpb.client.module.news.adapter.NewsItemListViewAdapterNew;
import cn.com.czpb.client.module.news.listener.RefreshDataListener;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/8/9.
 */

public class SearchNewsFragment extends BaseFragment implements RefreshDataListener,BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.rc_searchnews_id)
    RecyclerView rcSearchnewsId;
    private NewsItemListViewAdapterNew adapter;
    private boolean mFlagRefresh=true;

    private int page = 1;
    private static final int pageSize = 15;
    private String con ="";

    @Override
    public int setMyContentView() {
        return R.layout.fragment_searchnews_layout;
    }

    @Override
    public void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rcSearchnewsId.setLayoutManager(layoutManager);
        initAdapter();
    }

    /**
     *
     */
    private void initAdapter() {
        adapter = new NewsItemListViewAdapterNew(activity,null,false);
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this, rcSearchnewsId);
        rcSearchnewsId.setAdapter(adapter);

    }

    @Override
    public void initData() {
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        NewsBean newsBean = (NewsBean) adapter.getItem(position);
        switch (view.getId()){
            case R.id.newsItem_root:
                MyActivitymanager.pushActivity(activity, newsBean, "", "newsitem",newsBean.getRvalue());
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        mFlagRefresh=false;
        page++;
        showDialog();
        getSearchData(con);
    }


    private void getSearchData(String content) {
        Map<String, String> map = new HashMap<>();

        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("content", content);
        map.put("Page", page + "");
        map.put("PageSize", pageSize + "");
        Factory.provideHttpService().newsSearch(map)
                .subscribeOn(Schedulers.io())
                .filter(new Func1<NewsListEntity, Boolean>() {
                    @Override
                    public Boolean call(NewsListEntity entity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsListEntity>() {
                    @Override
                    public void call(NewsListEntity entity) {
                        disMissDialog();
                        if ("200".equals(entity.code)) {
                            setListData(entity.data);
                        }else{
                            setListData(null);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        adapter.loadMoreFail();
                    }
                });
    }
    public void setListData(List<NewsBean> list) {
        if (mFlagRefresh){
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        }else{
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->"+adapter.getData().size());
        if (list.size()<pageSize){
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void refreshData(String con) {
        getSearchData(con);
        this.con =con;
    }
}
